package com.anxinr.util;

import com.anxinr.enums.ExceptionEnum;
import com.anxinr.exception.LogicException;
import com.anxinr.pojo.dto.UserDTO;

/**
 * 当线程进入之后，自动存储当前登录用户信息
 *
 * @author anxinr
 */
public class UserInfoContext {
    /**当前线程信息*/
    private static final ThreadLocal<UserDTO> user = new ThreadLocal<>() ;
    private UserInfoContext(){}
    public static Long getUserId() {
        UserDTO baseUserDTO = getUser();
        return baseUserDTO.getId();
    }
    public static UserDTO getUser() {
        UserDTO baseUser = user.get();
        if (null == baseUser) {
            throw new LogicException(ExceptionEnum.STUDY_INVALID_LOGIN_ERROR);
        }
        return baseUser;
    }
    public static void setUserInfo(UserDTO baseUser) {
        user.set(baseUser);
    }
    public static void remove() {
        user.remove();
    }
}
